<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;


class FinanceBeforeKk extends Base
{
    protected $table = 'finance_room_before_kk_wealth';


    public function __construct(array $attributes = [])
    {
        $this->tablePrefix = env('DB_PREFIX');
        parent::__construct($this->table, $attributes);
    }

    public function getBeforeWealth($date)
    {
        $where['day'] = $date;
        $res = DB::table($this->table)->where($where)->get();
        return $this->objToArray($res);
    }

    //生成记录
    public function createBeforeWealth($data)
    {
        return DB::table($this->table)->insert($data);
    }

    //查询单条记录
    public function getBeforeWealthTotalDataLimit($id, $date)
    {
        $where['id'] = $id;
        $where['day'] = $date;
        $res = DB::table($this->table)->where($where)->first();
        return $this->objToArray($res);
    }

    public function updateBeforeWealthTotalDataLimit($id, $data)
    {
        $where['id'] = $id;
        return DB::table($this->table)->where($where)->update($data);
    }

    /*
     * $before_total //预支金额
     * */
    public function updateBeforeWealthTotalDataInc($id, $before_total)
    {
        $where['id'] = $id;
        DB::table($this->table)->where($where)->decrement('surplus_total', $before_total);// -
        return DB::table($this->table)->where($where)->increment('already_total', $before_total); // +
    }


    public function insertBeforeKkLog($data)
    {
        return DB::table('finance_before_kk_log')->insertGetId($data);
    }

}
